// pages/hadBeenMovie/hadBeenMovie.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hadBeenMovieList: [],
    writeOpen: 0,
    movieId: '',
    movieName: '',
    content: '',
    stars: [
      {
        flag: 1,
        bgImg: "/img/icon_bsta_no.png",
        bgfImg: "/img/icon_bstar1.png"
      },
      {
        flag: 1,
        bgImg: "/img/icon_bsta_no.png",
        bgfImg: "/img/icon_bstar1.png"
      },
      {
        flag: 1,
        bgImg: "/img/icon_bsta_no.png",
        bgfImg: "/img/icon_bstar1.png"
      },
      {
        flag: 1,
        bgImg: "/img/icon_bsta_no.png",
        bgfImg: "/img/icon_bstar1.png"
      },
      {
        flag: 1,
        bgImg: "/img/icon_bsta_no.png",
        bgfImg: "/img/icon_bstar1.png"
      }
    ],
    star_num:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    wx.request({
      url: 'http://localhost:8081/api/order/wxGetHadBeenMovieByUsername',
      data: {
        username: wx.getStorageSync('username')
      },
      method: 'GET',
      header: {
        'token' : wx.getStorageSync('token')
      },
      success:function(res){
        if (res.data.code == 10008) {
          that.setData({
            hadBeenMovieList : res.data.result
          })
        }
      }
    })
  },

  //跳转详情页
  toMovieDetail(e) {
    wx.navigateTo({
      url: '../movieDetail/movieDetail?movieId=' + e.currentTarget.dataset.tar,
    })
  },
  
  // 选择范围
  score(e) {
    var _this = this;
    for (var i = 0; i < _this.data.stars.length; i++) {
      var allItem = 'stars[' + i + '].flag';
      // 全部变为未选中状态
      _this.setData({
        [allItem]: 1
      })
    }
    var index = e.currentTarget.dataset.index;
    for (var i = 0; i <= index; i++) {
      var item = 'stars[' + i + '].flag';
      // 选中项及以前，变为选中状态
      _this.setData({
        [item]: 2,
      })
    }
    // 评分
    _this.setData({
      star_num: index + 1 // 评分
    })
  },

  // 写评论
  writeComment(e){
    var that = this
    console.log(e)
    that.setData({
      writeOpen : 1,
      movieId : e.currentTarget.dataset.tar.movieId,
      movieName : e.currentTarget.dataset.tar.movieName,
    })
  },

  inputContent(e){
    var that = this
    that.setData({
      content: e.detail.value
    })
  },

  submitContent(){
    var that = this
    var comment = {
      movieId : that.data.movieId,
      username: wx.getStorageSync('username'),
      content: that.data.content,
      startScore: that.data.star_num
    }
    wx.request({
      url: 'http://localhost:8081/api/comment/submitContent',
      data: comment,
      header: {
        'token': wx.getStorageSync('token')
      },
      method: 'POST',
      success:function(res){
        if (res.data.code == 10008) {
          wx.showToast({
            title: '评论成功',
          }),
          that.setData({
            writeOpen: 0,
            content: ''
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this
    wx.request({
      url: 'http://localhost:8081/api/order/wxGetHadBeenMovieByUsername',
      data: {
        username: wx.getStorageSync('username')
      },
      method: 'GET',
      header: {
        'token' : wx.getStorageSync('token')
      },
      success:function(res){
        if (res.data.code == 10008) {
          that.setData({
            hadBeenMovieList : res.data.result
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})